<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var numOfSubarrays = function (arr, k, threshold) {
      var num = 0
      var result = 0
      //  循环k次，将前k个数字依次累加
      for (var i = 0; i < k; i++) {
        num += arr[i]
      }
      // 因为上一个循环已经取得前k个数字之和，所以本次循环从k开始
      for (var i = k; i <= arr.length; i++) {
        if (num >= k * threshold) {
          result += 1
        }
        num -= arr[i - k]
        num += arr[i]
      }
      return result
    };
    console.log(numOfSubarrays([2, 2, 2, 2, 5, 5, 5, 8], 3, 4));
  </script>
</body>

</html>